{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1. , 1.1],\n",
       "        [1. , 1. ],\n",
       "        [0. , 0. ],\n",
       "        [0. , 0.1]]),\n",
       " array([0, 0, 1, 1]))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "x = np.array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]])\n",
    "y = np.array([0, 0, 1, 1])\n",
    "\n",
    "x, y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "def knn(_x, k=3):\n",
    "    temp = _x - x\n",
    "    temp = np.power(temp, 2)\n",
    "    temp = temp.sum(axis=1)\n",
    "    temp = np.sqrt(temp)\n",
    "    argsort = temp.argsort()\n",
    "    result = y[argsort][:k]\n",
    "    return np.bincount(result).argmax()\n",
    "\n",
    "\n",
    "for i in x:\n",
    "    print(knn(i))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
